<?php

namespace IDock\Insurance\JDAL\Request;

use IDock\Insurance\JDAL\Response\UnderwritingResponse;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\SpecialClausesVisitor;

class UnderwritingRequest extends AllJDALRequest
{
    public $route = 'insure/create';

    public $requestCode = 'quoteCreateRequest';

    protected $response = UnderwritingResponse::class;

    protected $urlTip = '投保接口';

    /*** @var  PremiumInquiryVisitor $visitor */
    protected $visitor;

    public function generate(): array
    {
        $this->partnerRef = $this->visitor->argc['channelCode'] ?? '';

        return $this->bindUnderWritingData();

    }

    private function bindUnderWritingData(): array
    {
        $data = $this->visitor->getExtInfoVisitor()->argc['underwritingData'] ?? '';

        $data['specialPromiseInfoList'] = $this->specialPromise();
        $data['basePart']['applicationNoType'] = '1';
        //险种格式化
        $data['coverageItemList'] = $this->coverageItemList($data['coverageItemList']);

        unset($data['quoteOperateType']);

        return $data;
    }

    /**
     * 特约
     * @return array
     */
    private function specialPromise(): array
    {
        $specialPromise = [];

        /*** @var $item SpecialClausesVisitor */
        foreach ($this->visitor->underwritingVisitor->specialClausesList as $item) {
            $specialPromise[] = [
                'clauseCode' => $item->clauseCode,
                'wordings' => $item->clauseName,
                'productLine' => $item->argc['productLine'] ?? '',
            ];
        }

        return $specialPromise;
    }

    /**
     * 险种格式化
     * @param $list
     * @return array
     */
    private function coverageItemList($list):array {
        $tList = [];
        foreach ($list as $item) {
            //去除交强
            if ($item['coverageCode'] == 'CTPL') {
                continue;
            }

            $data = [
                'premium' => $item['premium'],
                'coverageCode' => $item['coverageCode'],
                'sumLimit' => $item['sumLimit'],
            ];

            if (in_array($item['coverageCode'], ['ANPL','ANDL','ANMH','ANTPL'])) {
                $data['defaultDiscount'] = $item['sumLimit'];
            }

            if ($item['coverageCode'] == 'RC') {
                $data['compensationDays'] = $item['compensationDays'];
                $data['dailyCompensation'] = $item['dailyCompensation'];
            }

            $tList[] = $data;
        }
        return $tList;
    }
}
